package client_tests;

import net.es.lookup.common.exception.LSClientException;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 9/20/13
 * Time: 5:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestManager {

//    private static Logger LOG = Logger.getLogger(TestManager.class);
    private static String logConfig = "/home/luke/log4j.properties";
    private static Set<String> tests = new HashSet<String>(Arrays.asList("single-query", "multiple-regs", "sim-queries"));
    private static final int ARG_COUNT = 7;

    public static void main(String[] args) {

        System.setProperty("log4j.configuration", "file:" + logConfig);
        String sourceHost = null;
        String targetHost = null;
        int port = -1;
        String test = null;
        int op_interval = Integer.MAX_VALUE;
        int numThreads = -1;
        int testRounds = -1;

        try {

            if (args.length != ARG_COUNT) {

                throw new IllegalArgumentException();
            }
            sourceHost = args[0];
            targetHost = args[1];
            port = Integer.decode(args[2]);
            test = args[3];
            op_interval = Integer.decode(args[4]);
            numThreads = Integer.decode(args[5]);
            testRounds = Integer.decode(args[6]);

            if (port < 1 || numThreads < 0 || testRounds < 0) {

                throw new IllegalArgumentException();
            }

            if (!tests.contains(test)) {

                throw new Exception();
            }
        } catch (IllegalArgumentException e) {

            System.out.println("Usage: java -jar <jar-name> <source host> <target host> <port> <test> <operation_interval> <num_threads> <num_test_rounds>");
            if (port < 1 && args.length == ARG_COUNT) {

                System.out.println("<port> must be a positive integer");
            }
            if ((numThreads < 0 || testRounds < 0) && args.length == ARG_COUNT) {

                System.out.println("<numThreads> and <testRounds> must be integers >= 0");
            }
            if (op_interval == Integer.MAX_VALUE || numThreads == Integer.MAX_VALUE || testRounds == Integer.MAX_VALUE) {

                System.out.println("<operation_interval>, <num_threads>, and <num_test_rounds> must be integers");
            }
            System.exit(1);
        } catch (Exception e) {

            System.out.println("Usage: java -jar <jar-name> <source host> <target host> <port> <test> <operation_interval> <num_threads> <num_test_rounds>");
            System.out.println("\"" + args[3] + "\" is not a valid test name. Select from the following:");
            System.out.println(tests);
            System.exit(1);
        }

        ScalabilityTest st = null;
        if (test.equals("single-query")) {

            st = new SingleQuery();
        } else if (test.equals("multiple-regs")) {

            st = new MultipleRegistrations();
        } else if (test.equals("sim-queries")) {

            st = new SimultaneousQueries();
        }

        try {

            st.initialize(targetHost, port, sourceHost, op_interval, numThreads, testRounds);

            st.runTest();

            st.printResults();
        } catch (LSClientException e) {

            System.out.println("LSClientException occurred on " + sourceHost);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
